package com.amorcloud.pig.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amorcloud.pig.R;
import com.amorcloud.pig.bean.PigInfoBean;

import java.util.List;

/**
 查看猪只数据界面
 */
public class PigListAdapter extends BaseAdapter {

    List<PigInfoBean> mList;
    Context mContext;

    public PigListAdapter(Context context, List<PigInfoBean> list) {
        mList = list;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pig_data, null);
            holder = new ViewHolder();
            holder.farmId = convertView.findViewById(R.id.tv_farm_id);
            holder.hogId = convertView.findViewById(R.id.tv_hog_id);
            holder.dateTime = convertView.findViewById(R.id.tv_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PigInfoBean bean = mList.get(position);
        holder.farmId.setText(bean.getFarmID());
        holder.hogId.setText(bean.getHogID());
        holder.dateTime.setText(bean.getDateTime().substring(0,9));
        return convertView;
    }

    public void updateListViewData (List<PigInfoBean> list) {
        mList = list;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView farmId, hogId, dateTime;
    }
}
